
# 二分查找,条件是数组是有序的
# 迭代问题，注意函数闭包问题
def binary_search(array,item,i):
    lens=i
    mid=lens//2
    if mid>=1:
        if array[mid]==item:
            print('==={},item:{}'.format(array[mid],item))
            return mid
        elif array[mid]<item:  
            binary_search(array[mid:],item)
        else:
            binary_search(array[:mid],item)
    else:
        return -1
    

# 线性查找

def line_search(array,item):
    for i in range(0,len(array)):
        if array[i]== item:
            return i
    return -1

if __name__ == '__main__':
    array=('a','b','c','d','e','g',
    'h')
    array1=[1,2,5,7,9,12,19,23,34]
    # print(line_search(array,'d'))
    # print(binary_search(array,'d'))
    # print(line_search(array,'f'))
    # print(binary_search(array,'f'))

    print(line_search(array1,19))
    print(binary_search(array1,19))
    print(line_search(array1,29))
    print(binary_search(array1,29))



